<?php

namespace AdminModule;

use \Nette\Application\UI\Form;

class UsersPresenter extends \BasePresenter
{

	public function renderDefault()
	{
		$usersModel = $this->context->models->users;
		$this->template->users = $usersModel->getUsers();
	}

	public function renderAddUser()
	{
		$usersModel = $this->context->models->users;
		$skillsInDepartment = array();
		foreach($usersModel->getDepartments() as $department){
				$skillsInDepartment[$department['id']] = array();
		}
		foreach($usersModel->getSkills() as $skill){
				$skillsInDepartment[$skill['department_id']][] = $skill['id'];
		}
		$this->template->skillsInDepartment = $skillsInDepartment;
	}

	public function renderEditUser($id)
	{
		$usersModel = $this->context->models->users;
		$user = $usersModel->getUser(array('id' => $id));
		$this['editUserForm']->setValues($user);
		$skills = array();
		foreach ($usersModel->getUsersSkills(array('user_id' => $id)) as $skill)
		{
			$skills[] = $skill['skill_id'];
		}
		$this['editUserForm']['skills']->setValue($skills);

		$skillsInDepartment = array();
		foreach ($usersModel->getDepartments() as $department)
		{
			$skillsInDepartment[$department['id']] = array();
		}
		foreach ($usersModel->getSkills() as $skill)
		{
			$skillsInDepartment[$skill['department_id']][] = $skill['id'];
		}
		$this->template->skillsInDepartment = $skillsInDepartment;
	}

	/** @secured */
	public function handleDeleteUser($id)
	{
		$usersModel = $this->context->models->users;
		if ($usersModel->deleteUser($id))
		{
			$this->flashMessage('Uživatel byl úspěšně smazán.');
		} else
		{
			$this->flashMessage('Uživatel nelze smazat, pokud v systému již vykonal nějaké akce. Pouze ho v <a href="' . $this->link('editUser', $id) . '">nastavení</a> deaktivujte.', 'error');
		}
		$this->redirect('this');
	}

	public function createComponentAddUserForm($name)
	{
		$form = $this->generateUserForm($name);
		$form->onSuccess[] = callback($this, 'addUser');
		return $form;
	}

	public function createComponentEditUserForm($name)
	{
		$form = $this->generateUserForm($name);
		$form->addHidden('id');
		$form->onSuccess[] = callback($this, 'editUser');
		return $form;
	}

	protected function generateUserForm($name)
	{
		$authModel = $this->context->models->authorization;
		$departmentModel = $this->context->models->department;
		$usersModel = $this->context->models->users;
		$form = new Form($this, $name);
		$form->addText('name', 'Jméno:')
				->setRequired('Jméno je povinné');
		$form->addText('surname', 'Příjmení:')
				->setRequired('Příjmení je povinné');
		$form->addText('job', 'Pozice:');
		$form->addText('email', 'E-mail:')
				->addCondition(Form::FILLED)
				->addRule(Form::EMAIL, 'Byl zadán neplatný e-mail');
		$form->addText('login', 'Login:');
		$form->addPassword('password', 'Heslo:');
		$form->addPassword('password_again', 'Heslo znovu:')
				->addConditionOn($form['password'], Form::FILLED)
				->addRule(Form::EQUAL, 'Hesla se musí shodovat.', $form['password']);
		$form->addRadioList('active', 'Aktivní:')
				->setItems(array('yes' => 'Ano', 'no' => 'Ne'));
		$roles = $authModel->getRoles()->fetchPairs('id', 'name');
		$form->addSelect('user_role_id', 'Role:', $roles)
				->setPrompt('Vyberte roli')
				->setRequired('Role je povinná');
		$departments = $departmentModel->getDepartments()
				->fetchPairs('id', 'name');
		$form->addSelect('department_id', 'Oddělení:', $departments)
				->setPrompt('Vyberte oddělení');
		$skills = $usersModel->getSkills()->fetchPairs('id', 'name');
		$form->addCheckboxList('skills', 'Dovednosti:', $skills)
				->getControlPrototype()->class('cbli');
		$form->addSubmit('save', 'Uložit');
		return $form;
	}

	public function addUser($form)
	{
		$values = $form->getValues();
		$usersModel = $this->context->models->users;
		if ($values['password'] == "")
		{
			unset($values['password']);
		}
		unset($values['password_again']);

		if ($usersModel->addUser($values))
		{
			$this->flashMessage('Uživatel úspěšně přidán');
			$this->redirect('default');
		} else
		{
			$this->flashMessage('Chyba při přidávání uživatele. Zkontrolujte unikátnost přihlašovacího jména.', 'error');
		}
	}

	public function editUser($form)
	{
		$values = $form->getValues();
		$usersModel = $this->context->models->users;
		if ($values['password'] == "")
		{
			unset($values['password']);
		}
		unset($values['password_again']);

		if ($usersModel->editUser($values) !== false)
		{
			$this->flashMessage('Uživatel úspěšně upraven');
			$this->redirect('default');
		} else
		{
			$this->flashMessage('Chyba při úpravě uživatele.', 'error');
		}
	}

}